package com.aaa.service.sas.Impl;


import com.aaa.dao.sas.TSasPlanMapper;
import com.aaa.dao.sas.TSasRecordMapper;
import com.aaa.entity.MyPage;
import com.aaa.entity.User;
import com.aaa.entity.sas.TSasPlan;
import com.aaa.entity.sas.TSasRecord;
import com.aaa.service.sas.TSasPlanService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * (TSasPlan)表服务实现类
 *
 * @author makejava
 * @since 2022-11-04 00:48:04
 */
@Service
public class TSasPlanServiceImpl implements TSasPlanService {
   @Autowired
   private TSasPlanMapper tSasPlanMapper;
   @Autowired
   private TSasRecordMapper tSasRecordMapper;

   @Override
   public PageInfo queryAll(MyPage page, TSasPlan tSasPlan) {
      PageHelper.startPage(page.getPage(),page.getLimit());
      List<TSasPlan> tSasPlans = tSasPlanMapper.queryAll(tSasPlan);
      PageInfo pageInfo = new PageInfo(tSasPlans);
      return pageInfo;
   }

   @Override
   public Boolean checkCode(TSasPlan tSasPlan) {
      int i = tSasPlanMapper.selectCount(tSasPlan);
      System.out.println("验证码已经存在===========================i = " + i);
      return i==0?true:false;
   }

   @Override
   public Boolean checkPlanname(String planname) {
      TSasPlan tSasPlan = new TSasPlan();
      tSasPlan.setPlanname(planname);
      int i = tSasPlanMapper.selectCount(tSasPlan);
      System.out.println("该计划名字已经存在===========================i = " + i);
      return i==0?true:false;
   }

   @Override
   public Boolean addOrUpdataPlan(TSasPlan tSasPlan, User user) {
      int i = -1;
      if (tSasPlan.getId()==null){
         tSasPlan.setUid(user.getId());
         tSasPlan.setCreatetime(new Date());
         i = tSasPlanMapper.insert(tSasPlan);
      }else {
         tSasPlan.setUpdatetime(new Date());
         i = tSasPlanMapper.updateByPrimaryKeySelective(tSasPlan);
      }
      return i>0?true:false;
   }

   @Override
   public Boolean delPlan(Integer id, String code) {
      int i=-1;
      TSasRecord tSasRecord = new TSasRecord();
      tSasRecord.setYqcode(code);
      int i1 = tSasRecordMapper.selectCount(tSasRecord);
      if (i1 == 0){
         i = tSasPlanMapper.deleteByPrimaryKey(id);
      }
      return i>0?true:false;
   }

   @Override
   public Boolean updUserStatus(TSasPlan tSasPlan) {
      int i = tSasPlanMapper.updateByPrimaryKeySelective(tSasPlan);
      return i>0?true:false;
   }

   @Override
   public TSasPlan getByCode(TSasPlan tSasPlan) {
      tSasPlan.setStatus(1);
      TSasPlan tSasPlan1 = tSasPlanMapper.selectOne(tSasPlan);
      return tSasPlan1;
   }

   @Override
   public int getPlanId(String yqcode) {
      TSasPlan tSasPlan = new TSasPlan();
      tSasPlan.setCode(yqcode);
      TSasPlan tSasPlan1 = tSasPlanMapper.selectOne(tSasPlan);
      Integer planId = tSasPlan1.getId();
      System.out.println("获取到的计划的planId = " + planId);
      return planId;
   }
}
